package com.csun.cmny.provider.listener;

import com.csun.cmny.provider.constant.Constant;
import com.csun.cmny.provider.model.constant.MedicConstant;
import com.csun.cmny.provider.service.OmcMedicRecordService;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-08-13 15:56
 * @Description: the class omc medic listener impl
 */
@Slf4j
@Component
public class OmcMedicListenerImpl implements TaskListener, ExecutionListener {

    @Autowired
    private TaskService taskService;
    @Resource
    private OmcMedicRecordService omcMedicRecordService;

    @Override
    public void notify(DelegateExecution delegateExecution) {

        log.info("# medic listener delegate execution notify...{}", delegateExecution.toString());
    }

    @Override
    public void notify(DelegateTask delegateTask) {

        log.info("# medic listener delegate task notify...{}", delegateTask.toString());

        String processInstanceId = delegateTask.getProcessInstanceId();
        // String taskKey = delegateTask.getTaskDefinitionKey();

        Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
        String taskKey = task.getTaskDefinitionKey();

        switch (taskKey) {
            case MedicConstant.MEDIC_RECORD_TASK_APPLY :
                // 当前任务【发布计划】 下一个任务 【护工喂药】
                // taskService.addComment(delegateTask.getId(), processInstanceId, MedicConstant.MEDIC_RECORD_COMMENT_APPLY);
                // 改变状态 申请中
                omcMedicRecordService.applyFeedByProcessInstanceId(processInstanceId);
                break;
            case MedicConstant.MEDIC_RECORD_TASK_FEED :
                // 当前任务【护工喂药】 下一个任务 【超时处理】
                taskService.addComment(delegateTask.getId(), processInstanceId,
                        MedicConstant.MEDIC_RECORD_COMMENT_FEED_DELAY);
                // 改变状态 领取任务超时
                // 喂药超时
                omcMedicRecordService.feedDelayByProcessInstanceId(processInstanceId);
                break;
            case MedicConstant.MEDIC_RECORD_TASK_NURSING_CONFIRM :
                // 当前任务【护工确认】 下一个任务 【超时处理】
                taskService.addComment(delegateTask.getId(), processInstanceId,
                        MedicConstant.MEDIC_RECORD_COMMENT_NURSING_CONFIRM_DELAY);
                // 改变状态 护工确认超时
                // 护工确认超时
                omcMedicRecordService.nursingConfirmDelayByProcessInstanceId(processInstanceId);
                break;
            case MedicConstant.MEDIC_RECORD_TASK_MONITOR_CONFIRM :
                // 当前任务【班长确认】 下一个任务 【超时处理】
                taskService.addComment(delegateTask.getId(), processInstanceId,
                        MedicConstant.MEDIC_RECORD_COMMENT_MONITOR_CONFIRM_DELAY);
                // 改变状态 班长确认超时
                // 班长确认超时
                omcMedicRecordService.monitorConfirmDelayByProcessInstanceId(processInstanceId);
                break;
            default:
                log.error("# 无法识别的任务...[{}]", taskKey);
                break;
        }
    }
}
